package edu.nwpu.service.impl;

import edu.nwpu.dao.ManagerMapper;
import edu.nwpu.pojo.Manager;
;import edu.nwpu.service.ManagerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("managerService")
public class ManagerServiceImpl implements ManagerService {

    @Resource(name = "managerMapper")
    private ManagerMapper managerMapper;

    @Override
    public String login(String name, String password) {

        String loginStatus;

        if (name == null || "".equals(name)) {
            loginStatus = "管理员名为空";
        } else if (password == null || "".equals(password)) {
            loginStatus = "管理员密码为空";
        } else {  // 用户名和密码都不为空

            Manager m = managerMapper.getManagerByNameAndPassword(name, password);
            if (m == null) {
                loginStatus = "管理员名或密码错误";
            } else {
                loginStatus = "管理员登录成功";
            }
               }
            return loginStatus;

    }

    @Override
    public List<Manager> queryAllManagers(){

        return managerMapper.getAllManagers();
    }

    @Override
    public String addManager(Manager manager) {
        String addManagerStatus;

        if(manager == null || "".equals(manager.getName()) || "".equals(manager.getId())){
            addManagerStatus = "添加失败";
        }
        else if(managerMapper.getManagerById(manager.getId()) != null) {
            // 欲添加的管理员与已有管理员Id冲突
            addManagerStatus = "添加失败";
        }
        else {
            int ret = managerMapper.addManager(manager);
            if (ret > 0) {
                addManagerStatus = "添加成功";
            } else {
                addManagerStatus = "添加失败";
            }
        }
        return addManagerStatus;
    }

    @Override
    public String deleteManagerById(Long id) {
        String deleteManagerStatus1;

        int ret = managerMapper.deleteManagerById(id);
        if (ret > 0) {
            deleteManagerStatus1 = "删除成功";
        } else {
            deleteManagerStatus1 = "添加失败";
        }
        return deleteManagerStatus1;

    }

    @Override
    public String deleteManagerByName(String name) {
        /*String deleteManagerStatus2;

        int ret = managerMapper.deleteManagerByName(name);
        if (ret > 0) {
            deleteManagerStatus2 = "删除成功";
        } else {
            deleteManagerStatus2 = "添加失败";
        }
        return deleteManagerStatus2;*/
        return null;
    }

    @Override
    public String updateManager(Manager newManager) {
        String updateManagerStatus;

        if(newManager == null){
            updateManagerStatus = "更新失败";
        }
        else if (managerMapper.getManagerById(newManager.getId()) == null){
            // 欲更新的管理员Id不存在
            updateManagerStatus = "更新失败";
        }
        else{

            int ret = managerMapper.updateManager(newManager);
            if (ret > 0) {
                updateManagerStatus = "更新成功";
            } else {
                updateManagerStatus = "更新失败";
            }
        }
        return updateManagerStatus;
    }

}
